package com.dhnsoft.education.controller.userCenter;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.userCenter.UcenterMember;
import com.dhnsoft.education.entity.userCenter.vo.PwdVo;
import com.dhnsoft.education.entity.userCenter.vo.RegisterVo;
import com.dhnsoft.education.service.userCenter.UcenterMemberService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.R;
import com.dhnsoft.education.vo.UcenterMemberInfoCommonVo;
import com.dhnsoft.education.vo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
        //调用service方法实现登陆
        //返回token值，使用jwt方法生成
        String token = memberService.login(ucenterMember);
        return R.ok().data("token",token);
    }



    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        boolean register = memberService.register(registerVo);
        if(register){
            return R.ok();
        }
        else {
            return R.error();
        }

    }
    //更改密码的方法
    @PostMapping("updatePwd")
    public R updatePwd(@RequestBody PwdVo pwdVo){
        boolean result = memberService.updatePwd(pwdVo);
        if(result){
            return R.ok();
        }
        else {
            return R.error();
        }

    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据service_edu的远程调用接口 通过用户id来查询用户信息
    @GetMapping("getUserInfo/{id}")
    public UcenterMemberInfoCommonVo getUserInfo(@PathVariable String id){
        //根据用户id来查询数据
        UcenterMember userInfo = memberService.getById(id);
        UcenterMemberInfoCommonVo infoCommonVo = new UcenterMemberInfoCommonVo();
        BeanUtils.copyProperties(userInfo,infoCommonVo);
        System.out.println(infoCommonVo);
        return infoCommonVo;
    }
    //根据用户id获取用户信息  远程调用 service-order调用
    @GetMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        //根据用户id来查询数据
        UcenterMember userInfo = memberService.getById(id);
        UcenterMemberOrder infoCommonVo = new UcenterMemberOrder();
        BeanUtils.copyProperties(userInfo,infoCommonVo);
        return infoCommonVo;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }

    //条件带分页查询客户信息
    @PostMapping("pageUserInfo/{current}/{limit}")
    public R pageUserInfo(@PathVariable long current, @PathVariable long limit, @RequestBody UcenterMember userQuery){
        Page<UcenterMember> memberPage = new Page<>(current,limit);
        //构建条件
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        //多条件查询
        String nickname = userQuery.getNickname();
        String email = userQuery.getEmail();
        Integer sex = userQuery.getSex();
        if(!StringUtils.isEmpty(nickname)){
            wrapper.like("nickname",nickname);
        }
        if(!StringUtils.isEmpty(email)){
            wrapper.like("email",email);
        }
        if(!StringUtils.isEmpty(sex)){
            wrapper.eq("sex",sex);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页条件查询
        memberService.page(memberPage,wrapper);
        long total = memberPage.getTotal();//总记录数
        List<UcenterMember> records = memberPage.getRecords();//数据list集合数
        return R.ok().data("total",total).data("rows",records);
    }

    //根据用户id修改客户信息
    @PostMapping("updateUserInfoById")
    public R updateUserInfoById(@RequestBody UcenterMember userInfo){
        memberService.updateById(userInfo);
        return R.ok();
    }

    //根据用户id删除客户
    @GetMapping("delUserById/{id}")
    public R delUserById(@PathVariable String id){
        memberService.removeById(id);
        return R.ok();
    }

    //根据用户id查询
    @GetMapping("getUserById/{id}")
    public R getUserById(@PathVariable long id){
        UcenterMember byId = memberService.getOne(new QueryWrapper<UcenterMember>().eq("id",id));
        System.out.println(byId);
        return R.ok().data("user",byId);
    }

    //用户上传头像
    @PostMapping("uploadAvatar")
    public R uploadAvatar(@RequestBody UcenterMember userInfo){
        memberService.updateById(userInfo);
        return R.ok();
    }
}

